package com.sky.controller.utis;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart/")
@Slf4j
@Api(tags = "购物车管理")
public class ShoppingCartContrroller {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> showShoppingCart() {
        log.info("查看购物车");
        List<ShoppingCart> shoppingCarts = shoppingService.showShoppingCart();
        return Result.success(shoppingCarts);
    }

    /**
     * 添加到购物车
     *
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加到购物车")
    public Result add(@RequestBody ShoppingCartDTO dto) {
        log.info("添加到购物车" + dto);
        return shoppingService.add(dto);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result deleteShopping() {
        shoppingService.deleteShopping();
        return Result.success();
    }

    /**
     * if>1,-1.if=1 ,delete
     * @return
     */
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO dto){
        return shoppingService.sub(dto);
    }
}
